package com.owl.app;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Dialog;
import android.content.Context;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

class noticeMainItem{
	String subject;
	String reg_date;
	noticeMainItem(String subject_tmp, String reg_date_tmp){
		subject = subject_tmp;
		reg_date = reg_date_tmp;
	}
}

public class NoticeListAdapter extends BaseAdapter {
	
	LayoutInflater inflater;
	ArrayList<noticeMainItem> noticeMA;
	HashMap<String, String> hm;
	Context mContext;
	int mListLayout;
	private int[] listRowColor;
	public String TAG = "NoticeListAdapter";
	
	public NoticeListAdapter(Context tContext, int listLayout, ArrayList<noticeMainItem> noticeMAT){
		mContext = tContext;
		mListLayout = listLayout;
		noticeMA = noticeMAT;
		inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		listRowColor = new int[]{
				tContext.getResources().getColor(R.color.list_row_bg),
				tContext.getResources().getColor(R.color.list_row_bg_alt) };
		
	}
	
	public NoticeListAdapter(Context tContext, int listLayout, HashMap<String, String> hmTmp){
		mContext = tContext;
		mListLayout = listLayout;
		hm = hmTmp;
		inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		listRowColor = new int[]{
				tContext.getResources().getColor(R.color.list_row_bg),
				tContext.getResources().getColor(R.color.list_row_bg_alt) };
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return noticeMA.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return noticeMA.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		if(convertView == null){
			convertView = inflater.inflate(mListLayout, parent, false); 
		}
		
		/*TextView notice_subject = (TextView)convertView.findViewById(R.id.notice_subject);
		notice_subject.setText(noticeMA.get(position).subject);
		
		TextView notice_reg_date = (TextView)convertView.findViewById(R.id.notice_reg_date);
		notice_reg_date.setText(noticeMA.get(position).reg_date);*/
		
		TextView notice_subject = (TextView)convertView.findViewById(R.id.notice_subject);
		notice_subject.setText(hm.get("rowid[" + position + "]") + ". " + noticeMA.get(position).subject);
		
		TextView notice_reg_date = (TextView)convertView.findViewById(R.id.notice_reg_date);
		notice_reg_date.setText(noticeMA.get(position).reg_date);
		
		int colorPos = position % listRowColor.length;
		convertView.setBackgroundColor(listRowColor[colorPos]);
		
		final int positionInt = position;
		
		ImageButton noticeDetailBtn = (ImageButton)convertView.findViewById(R.id.noticeDetailBtn);
		
		try{
			noticeDetailBtn.setOnClickListener(new Button.OnClickListener(){
				public void onClick(View v){
					noticeInfo(positionInt);
				}
			});
		}catch(Exception e){
			Log.e(TAG, e.getMessage());
		}
		
		return convertView;
	}
	
	public void noticeInfo(int pos){
		
		Dialog dialog = new Dialog(mContext);
		dialog.setContentView(R.layout.notice_detail);
		dialog.setTitle("공지사항");
		TextView noticeDetailSubject = (TextView)dialog.findViewById(R.id.noticeDetailSubject);
		noticeDetailSubject.setText(hm.get("rowid[" + pos + "]") + "." + hm.get("subject[" + pos + "]"));
		TextView noticeDetailInfo = (TextView)dialog.findViewById(R.id.noticeDetailInfo);
		noticeDetailInfo.setText(hm.get("reg_date[" + pos + "]"));
		TextView noticeDetailContext = (TextView)dialog.findViewById(R.id.noticeDetailContent);
		noticeDetailContext.setText(hm.get("content[" + pos + "]"));
		noticeDetailContext.setMovementMethod(new ScrollingMovementMethod());
		dialog.show();
		Button buttonOK = (Button)dialog.findViewById(R.id.noticeDailtOKBtn);
		buttonOK.setOnClickListener(new noticeDetailOKListener(dialog));
	}
	
	protected class noticeDetailOKListener implements OnClickListener{
		private Dialog dialog;
		
		public noticeDetailOKListener(Dialog dialog){
			this.dialog = dialog;
		}
		public void onClick(View v){
			dialog.dismiss();
		}
	}

}
